package com.qf.service.impl;

import com.qf.entity.Product;
import com.qf.entity.Shop;
import com.qf.entity.User;
import com.qf.entity.custom.ShopCustom;
import com.qf.mapper.ProductMapper;
import com.qf.mapper.ShopMapper;
import com.qf.mapper.custom.ShopCustomMapper;
import com.qf.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ProjectName: Dessert-Shop
 * @Package: com.qf.service.impl
 * @ClassName: ShopServiceImpl
 * @Author: 29284
 * @Description: 店铺
 * @Date: 2020/7/26 19:18
 * @Version: 1.0
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopCustomMapper shopCustomMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 后台登陆
     *
     * @param shopLogin 对应shop实体中的shopLogin
     * @param shopPwd   对应shop实体中的shopPwd
     * @return 返回一个shop实体类型数据
     */
    @Override
    public Shop login(String shopLogin, String shopPwd) {
        if (shopLogin != null && shopLogin != "" && shopPwd != null && shopPwd != "") {
            return shopMapper.selectShop(shopLogin, shopPwd);
        } else {
            return null;
        }
    }


    /**
     * 后台商品注册
     * 判断shopLogin和shopLogin是否为空或者空字符串 如果是，则返回false，如果失败，则返回true
     * 把shopLogin和shopLogin封装到shop实体对象中 调用逆向生成的条件添加方法 判断返回值是否为1
     * 如果为1 则返回true 如果不为1 则返回false
     *
     * @param shop 对应shop实体
     * @return 注册成功返回true 失败返回false
     */
    @Override
    public Boolean register(Shop shop) {
        String shopLogin = shop.getShopLogin();
        String shopPwd = shop.getShopPwd();
        if (shopLogin != null && shopLogin != "" && shopPwd != null && shopPwd != "") {
            Shop shop1 = shopMapper.selectShop(shopLogin, "");
            if (shop1 == null) {
                int i = shopMapper.insertSelective(shop);
                if (i == 1) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 后台根据店铺查商品
     * @param shopId
     * @return
     */
    @Override
    public List<ShopCustom> getShopProductByShopId(int shopId) {

        List<ShopCustom> shopCustoms = shopCustomMapper.selectShopProduct(shopId);

        if (shopCustoms != null) {
            return shopCustoms;
        } else {
            return null;
        }
    }

    /**
     * 后台根据id改
     * @param product
     * @return
     */
    @Override
    public boolean modifyShopProductByShopId(Product product) {
        int i = productMapper.updateByPrimaryKeySelective(product);
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 后台根据id改
     * @param product
     * @return
     */
    @Override
    public boolean deleteShopProductByShopId(Product product) {
        product.setIsdel(1);
        int i = productMapper.updateByPrimaryKeySelective(product);
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 后台根据商品名查商品
     * @param shopId
     * @return
     */
    @Override
    public List<ShopCustom> getShopProductByTitle(int shopId,String productName) {

        List<ShopCustom> shopCustoms = shopCustomMapper.selectShopProductTitle(shopId, productName);

        if (shopCustoms != null) {
            return shopCustoms;
        } else {
            return null;
        }
    }

    /**
     * 后台根据店铺查商品
     * @param shopId
     * @return
     */
    @Override
    public List<ShopCustom> getShopProductByType(int shopId,String productType) {

        List<ShopCustom> shopCustoms = shopCustomMapper.selectShopProductType(shopId, productType);

        if (shopCustoms != null) {
            return shopCustoms;
        } else {
            return null;
        }
    }

    /**
     * 获取店铺信息
     * @param shopId 店铺id
     * @return 返回店铺实体类
     */
    @Override
    public Shop getShopById(int shopId){
        Shop shop = shopMapper.selectByPrimaryKey(shopId);
        if (shop != null){
            return shop;
        }else {
            return null;
        }
    }

    /**
     * 后台根据id改店铺信息
     * @param shop
     * @return
     */
    @Override
    public boolean modifyShopById(Shop shop) {
        int i = shopMapper.updateByPrimaryKeySelective(shop);
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 总顾客
     * @return
     */
    @Override
    public int getUserShopCount(int shopId){
        int i = shopCustomMapper.selectShopUserCount(shopId);
        return i;
    }

    /**
     * 新增顾客
     * @param createtime
     * @return
     */
    @Override
    public int getUserShopCountIsNew(int shopId,Date createtime){
        int i = shopCustomMapper.selectShopUserCountIsNew(shopId,createtime);
        return i;
    }


}
